package com.zhangmeng.tools.controller;

import cn.hutool.core.lang.Console;
import com.zhangmeng.tools.json2Pojo.AnnotateConfig;
import com.zhangmeng.tools.json2Pojo.Json2PojoCodeGeneratorConfig;
import com.zhangmeng.tools.json2Pojo.Json2PojoCodeGeneratorUtil;
import com.zhangmeng.tools.utils.AlertUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-04-21 10:01
 */
public class Json2PoJoController {

    @FXML
    public ComboBox<EnableBean> lombook_enable;

    @FXML
    public ComboBox<EnableBean> mybatis_plus_enable;

    @FXML
    public ComboBox<EnableBean> swagger_enable;

    @FXML
    public Button convert;

    public static int index = 1;

    @FXML
    public TextField class_name;

    @FXML
    public TextField package_name;

    @FXML
    public TextArea json_content;

    @FXML
    public TextArea result_show;

    @FXML
    public void initialize() {

        ObservableList<EnableBean> lombook_enable_list = FXCollections.observableArrayList();
        lombook_enable_list.add(new EnableBean("开启",true));
        lombook_enable_list.add(new EnableBean("关闭",false));
        lombook_enable.setItems(lombook_enable_list);
        lombook_enable.getSelectionModel().select(index);
        lombook_enable.setConverter(new StringConverter<EnableBean>() {
            @Override
            public String toString(EnableBean object) {

                if (object != null){
                    return object.title;
                }

                return null;
            }

            @Override
            public EnableBean fromString(String string) {
                return null;
            }
        });

        ObservableList<EnableBean> mybatis_plus_enable_list = FXCollections.observableArrayList();
        mybatis_plus_enable_list.add(new EnableBean("开启",true));
        mybatis_plus_enable_list.add(new EnableBean("关闭",false));
        mybatis_plus_enable.setItems(mybatis_plus_enable_list);
        mybatis_plus_enable.getSelectionModel().select(index);
        mybatis_plus_enable.setConverter(new StringConverter<EnableBean>() {
            @Override
            public String toString(EnableBean object) {

                if (object != null){
                    return object.title;
                }

                return null;
            }

            @Override
            public EnableBean fromString(String string) {
                return null;
            }
        });

        ObservableList<EnableBean> swagger_enable_list = FXCollections.observableArrayList();
        swagger_enable_list.add(new EnableBean("开启",true));
        swagger_enable_list.add(new EnableBean("关闭",false));
        swagger_enable.setItems(swagger_enable_list);
        swagger_enable.getSelectionModel().select(index);

        swagger_enable.setConverter(new StringConverter<>() {
            @Override
            public String toString(EnableBean object) {

                if (object != null) {
                    return object.title;
                }

                return null;
            }

            @Override
            public EnableBean fromString(String string) {
                return null;
            }
        });

        convert.setOnAction(event -> {

            if (class_name.getText().length() == 0){
                AlertUtils.alert_warning("类名不能为空!");
                return;
            }
            if (package_name.getText().length() == 0){
                AlertUtils.alert_warning("包名不能为空!");
                return;
            }

            if (json_content.getText().length() == 0){
                AlertUtils.alert_warning("json内容不能为空!");
                return;
            }

            String pojoCode = Json2PojoCodeGeneratorUtil.json2pojo(
                    Json2PojoCodeGeneratorConfig.builder()
                            .pojoClassName(class_name.getText())
                            .pojoPackageName(package_name.getText())
                            .jsonContent(json_content.getText()).build(),
                    AnnotateConfig.builder()
                            .mybatis_plus_enable(mybatis_plus_enable.getSelectionModel().getSelectedItem().isEnable())
                            .swagger_enable(swagger_enable.getSelectionModel().getSelectedItem().isEnable())
                            .lombook_enable(lombook_enable.getSelectionModel().getSelectedItem().isEnable()).build()
                    );
            result_show.setText(pojoCode);
        });

    }

    public static class EnableBean {
        private String title;//开启关闭
        private boolean isEnable;

        public EnableBean(String title, boolean isEnable) {
            this.title = title;
            this.isEnable = isEnable;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public boolean isEnable() {
            return isEnable;
        }

        public void setEnable(boolean enable) {
            isEnable = enable;
        }
    }
}
